package com.example.servicessepush.controller;

import com.example.internalcommon.util.SsePrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lyra
 * @Date 2023/4/30 14:52
 **/
@RestController
@Slf4j
public class SseController {

    public static Map<String, SseEmitter> sseEmitterMap = new HashMap<>();

    /**
     * 43.建立连接
     *
     * @param userId
     * @param identity
     * @return
     */
    @GetMapping("/connect")
    public SseEmitter connect(Long userId, String identity) {
        log.info("用户ID:" + userId + "身份类型:" + identity);
        String sseMapKey = SsePrefixUtils.generatorSeeKey(userId, identity);

        SseEmitter sseEmitter = new SseEmitter(0l);
        sseEmitterMap.put(sseMapKey, sseEmitter);
        return sseEmitter;
    }

    /**
     * 44.发送消息
     *
     * @param userId   消息接收者
     * @param identity 身份信息
     * @param content  消息内容
     * @return
     */
    @GetMapping("/push")
    public String push(@RequestParam Long userId, @RequestParam String identity, @RequestParam String content) {
        log.info("userId:" + userId + "identity:" + identity);
        String sseMapKey = SsePrefixUtils.generatorSeeKey(userId, identity);
        try {
            if (sseEmitterMap.containsKey(sseMapKey)) {
                sseEmitterMap.get(sseMapKey).send(content);
            } else {
                return "推送失败";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "给用户:" + sseMapKey + ",发送了消息:" + content;
    }

    /**
     * 45.关闭连接
     *
     * @param userId   用户ID
     * @param identity 身份标识
     * @return
     */
    @GetMapping("/close")
    public String close(@RequestParam Long userId, @RequestParam String identity) {
        String sseMapKey = SsePrefixUtils.generatorSeeKey(userId, identity);
        System.out.println("关闭连接:" + sseMapKey);
        if (sseEmitterMap.containsKey(sseMapKey)) {
            sseEmitterMap.remove(sseMapKey);
        }
        return "close 成功";
    }
}
